package com.example.music.ui;

import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Room;

import android.os.Bundle;
import android.text.TextUtils;

import com.example.music.databinding.ActivityRegisterBinding;
import com.example.music.room.AppDatabase;
import com.example.music.room.User;
import com.example.music.utlis.ToastUtil;


public class RegisterActivity extends AppCompatActivity {

    ActivityRegisterBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityRegisterBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.registerSureTv.setOnClickListener(v -> {
            String registerphone=binding.registerPhoneEt.getText().toString();
            String registerpwd=binding.registerPasswordEt.getText().toString();
            String registerpwd2=binding.registerPasswordEt2.getText().toString();
            if (TextUtils.isEmpty(registerphone)){
                ToastUtil.showShort(getBaseContext(),"请输入用户名");
            }else if(TextUtils.isEmpty(registerpwd)){
                ToastUtil.showShort(getBaseContext(),"请输入密码!");
            }else if(registerpwd.length()<6){
                ToastUtil.showShort(getBaseContext(),"密码不得小于6位");
            }else if(!registerpwd.equals(registerpwd2)){
                ToastUtil.showShort(getBaseContext(),"两次密码不一致,请重新输入!");
            }else{
                AppDatabase db = Room.databaseBuilder(getBaseContext(), AppDatabase.class, "database-name").build();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        User registerUser = new User(registerphone,registerpwd,registerphone);
                        db.userDao().insert(registerUser);
                        finish();
                    }


                }).start();
            }
        });
    }
}